package com.lzz.bottomencapsulation.mvp;

import android.content.Context;
import android.os.Bundle;
import com.lzz.bottomencapsulation.app.BaseFragment;
import java.lang.reflect.ParameterizedType;

import androidx.annotation.Nullable;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public  abstract class LBMVPBaseFragment<V extends LBBaseView, T extends LBBasePresenterImpl<V>> extends
        BaseFragment implements  LBBaseView{
    public T mPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = getInstance(this, 1);
        mPresenter.attachView((V) this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) mPresenter.detachView();
    }

    @Override
    public Context getContext() {
        return super.getContext();
    }

    @Override
    public Context getApplicationContext() {
        return getActivity().getApplicationContext();
    }

    @Override
    public void getSourceData() {
        mPresenter.getSourceData();
    }

    public <T> T getInstance(Object o, int i) {
        try {
            return ((Class<T>) ((ParameterizedType) (o.getClass().getGenericSuperclass()))
                    .getActualTypeArguments()[i]).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassCastException e) {
            e.printStackTrace();
        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }
}
